package com.magenta.maxoptra.gis;

import com.magenta.maxoptra.gis.util.Apis;

import javax.persistence.Column;

public class ReverseGeocodingPK {

    public Long cid;
    @Column
    public Integer latitude1;
    @Column
    public Integer longitude1;

    public ReverseGeocodingPK() { }

    public ReverseGeocodingPK(Long cid, GeographicCoordinates geographicCoordinates) {
    	this.cid = cid;
    	this.latitude1 = geographicCoordinates.getLatitudeInteger();
    	this.longitude1 = geographicCoordinates.getLongitudeInteger();
    }

    public ReverseGeocodingPK(String providerName, String versionApi, String cidDiscriminator, GeographicCoordinates geographicCoordinates) {
    	this(Apis.cid(providerName, versionApi, cidDiscriminator), geographicCoordinates);
    }

    private int hash(Object o) {
        return o != null ? o.hashCode() : 0;
    }

    @Override
    public int hashCode() {
        int result = (hash(cid) << 5 | hash(cid) >> 32 - 5);
        result ^= (hash(latitude1) << 11 | hash(latitude1) >> 32 - 11);
        result ^= (hash(longitude1) << 19 | hash(longitude1) >> 32 - 19);
        return result;
    }

    private boolean eq(Object obj1, Object obj2) {
        return obj1 == obj2 || (obj1 != null && obj1.equals(obj2));
    }

    private boolean vectorEq(ReverseGeocodingPK pk) {
        return eq(cid, pk.cid) && eq(latitude1, pk.latitude1) && eq(longitude1, pk.longitude1);
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof ReverseGeocodingPK && vectorEq((ReverseGeocodingPK) obj);
    }

	/**
	 * As array representation.
	 */
	public Object[] toArray() {
		return new Object[] {cid, latitude1, longitude1};
	}
}
